package org.momo.rpc.bootstrap;


import lombok.extern.slf4j.Slf4j;
import org.momo.rpc.client.ChannelHolder;
import org.momo.rpc.config.config.RpcConfig;
import org.momo.rpc.config.RpcConfigLoader;
import org.momo.rpc.config.config.registry.ZkRegistryConfig;
import org.momo.rpc.extention.ExtensionLoader;
import org.momo.rpc.registry.ConsumerRegistry;

@Slf4j
public class ConsumerBootstrap {

    public static void init() throws Exception {
        RpcConfigLoader.init();

        RpcConfig rpcConfig = RpcConfigLoader.getRpcConfig();

        // 注册中心初始化
        ZkRegistryConfig zkRegistryConfig = null;
        ConsumerRegistry consumerRegistry = null;

        consumerRegistry = ExtensionLoader.getExtensionLoader(ConsumerRegistry.class).getExtension(rpcConfig.getRegistry());
        zkRegistryConfig = rpcConfig.getRegistryConfig();
        consumerRegistry.initRegistryClient(zkRegistryConfig);

        // 创建并注册Shutdown Hook, JVM 退出时执行
        Runtime.getRuntime().addShutdownHook(new Thread(ChannelHolder::closeAllChannel));
        // Runtime.getRuntime().addShutdownHook(new Thread(consumerRegistry::closeRegistryClient));
    }

}
